package com.google.code.porntube.snap.analyzer;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLDecoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class Post {

	public static com.google.code.porntube.snap.result.Config analyze(String link) throws Exception {
		com.google.code.porntube.snap.result.Config result = new com.google.code.porntube.snap.result.Config();
		URL url = new URL(link);
        BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
        String line;
        StringBuffer html = new StringBuffer();

        while ((line = reader.readLine()) != null) {
        	html.append(line);
        }
        reader.close();
        
        Pattern htmlPattern = Pattern.compile("so\\.addVariable\\('config'.+?'\\);");
        Matcher matcher = htmlPattern.matcher(html);
        if(matcher.find()) {
        	result = Config.analyze(URLDecoder.decode(matcher.group(0).replace("so.addVariable('config', '", "").replace("');", ""), "UTF-8"));
        }
		return result;
	}
	
}
